<?php

/**
 * DTOAN
 */
class SettingForm extends CFormModel {

    //general
    public $facebook;
    public $google;
    public $twitter;
    public $logo;
    public $link_youtube;
    public $link_twitter;
    public $image_watermark;
    public $title;
    public $meta_description;
    public $meta_keywords;    
    public $hot_line;
    public $email;
    public $linkin;
    public $phone;
    public $text_service1;
    public $text_service2;
    public $text_blog1;
    public $text_blog2;
    public $text_header_contact;

    public function rules() {
        $return = array();
        foreach (SettingForm::$arrGeneral as $key => $value):
            $return[] = array($value, 'safe');
        endforeach;
        $return [] = array('facebook', 'length', 'max' => 200);
        $return [] = array('logo', 'file', 'on' => 'updateSettings',
            'allowEmpty' => true,
            'types' => 'jpg,gif,png,tiff',
            'wrongType' => 'Only jpg,gif,png,tiff allowed',
            'maxSize' => 1024 * 1024 * 3, // 8MB
            'tooLarge' => 'The file was larger than 3MB. Please upload a smaller file.',
        );
        $return [] = array('logo', 'match', 'pattern' => '/^[^\\/?*:&;{}\\\\]+\\.[^\\/?*:&;{}\\\\]{3}$/', 'message' => 'Image files name cannot include special characters: &%$#', 'on' => 'updateSettings');
        $return [] = array('text_header_contact,text_service1, text_service2, text_blog1, text_blog2, link_youtube, link_twitter, linkin, phone, email, facebook, phone', 'safe');
        return $return;
    }

    public static $arrGeneral = array(
        'email' => 'email',
        'phone' => 'phone',
        'linkin' => 'linkin',
        'facebook' => 'facebook',
        'google' => 'google',
        'logo' => 'logo',
        'link_youtube'=>'link_youtube',
        'link_twitter' => 'link_twitter',
        'title' => 'title',
        'meta_description' => 'meta_description',
        'meta_keywords' => 'meta_keywords',       
        'hot_line' => 'hot_line',
        'text_service1' => 'text_service1', 
        'text_service2' => 'text_service2', 
        'text_blog1' => 'text_blog1', 
        'text_blog2' =>'text_blog2',
        'text_header_contact' => 'text_header_contact'
    );

    /**
     * Override configurations.
     */
    static public function applySettings() {
        //apply setting for general
        foreach (self::$arrGeneral as $key => $value) {
            if (Yii::app()->setting->getItem($value)) {
                Yii::app()->params[$key] = Yii::app()->setting->getItem($value);
            }
        }

    }

}
